from typing import Sequence

from celery import shared_task
from django.conf import settings
from django.core.mail import send_mail


@shared_task(
    name="payments.mail.send_order_success_email",
    autoretry_for=(Exception,),
    retry_backoff=True,
    retry_jitter=True,
    max_retries=3,
)
def send_order_success_email(to_email: str,username: str,order_number: str,course_names: Sequence[str],) -> None:
    """发送订单支付成功通知邮件。"""
    subject = "购买成功通知"
    course_lines = "\n".join(f"- {name}" for name in course_names)
    message = (
        f"亲爱的 {username}, 您好！\n\n"
        f"您的订单已支付成功。订单号：{order_number}\n"
        f"购买课程：\n{course_lines}\n\n"
        f"祝您学习愉快！"
    )
    from_email = getattr(settings, "DEFAULT_FROM_EMAIL", "noreply@example.com")
    send_mail(
        subject=subject,
        message=message,
        from_email=from_email,
        recipient_list=[to_email],
        fail_silently=False,
    )

